home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / interp / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  4.7 KB  |  176 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: misc.c,v 1.11 94/11/03 22:19:24 wlott Exp $
  27. *
  28. * This file implements the stuff we couldn't think of anyplace
  29. * better to put.
  30. *
  31. \**********************************************************************/
  32.  
  33. #include "../compat/std-c.h"
  34.  
  35. #include "mindy.h"
  36. #include "thread.h"
  37. #include "bool.h"
  38. #include "list.h"
  39. #include "vec.h"
  40. #include "func.h"
  41. #include "obj.h"
  42. #include "module.h"
  43. #include "sym.h"
  44. #include "def.h"
  45. #include "num.h"
  46.  
  47. static struct variable *generic_apply_var = NULL;
  48.  
  49.  
  50. static obj_t dylan_exit(obj_t exit_value)
  51. {
  52.     exit(fixnum_value(exit_value));
  53. }
  54.  
  55. static void dylan_values(struct thread *thread, int nargs)
  56. {
  57.     obj_t *args = thread->sp - nargs;
  58.     do_return(thread, args-1, args);
  59. }
  60.  
  61. static void dylan_apply(struct thread *thread, int nargs)
  62. {
  63.     obj_t *args = thread->sp - nargs;
  64.     obj_t *old_sp = args-1;
  65.     obj_t *src = args;
  66.     obj_t *dst = old_sp;
  67.     obj_t *end = thread->sp - 1;
  68.     obj_t seq = *end;
  69.     obj_t class = object_class(seq);
  70.     boolean vector;
  71.  
  72.     if (!(vector = (class == obj_SimpleObjectVectorClass))
  73.     && class != obj_EmptyListClass && class != obj_PairClass) {
  74.     /* It isn't a simple-object-vector nor a list, we have to defer. */
  75.     *dst++ = generic_apply_var->value;
  76.     while (src < end)
  77.         *dst++ = *src++;
  78.     *dst++ = *src;
  79.     }
  80.     else {
  81.     /* Copy the function and the first n-1 args down the stack. */
  82.     while (src < end)
  83.         *dst++ = *src++;
  84.  
  85.     /* Spread the collection out on the stack. */
  86.     if (vector) {
  87.         src = obj_ptr(struct sovec *, seq)->contents;
  88.         end = src + obj_ptr(struct sovec *, seq)->length;
  89.         while (src < end)
  90.         *dst++ = *src++;
  91.     }
  92.     else {
  93.         while (seq != obj_Nil) {
  94.         *dst++ = HEAD(seq);
  95.         seq = TAIL(seq);
  96.         }
  97.     }
  98.     }
  99.     thread->sp = dst;
  100.     invoke(thread, dst - args);
  101. }
  102.  
  103. static void dylan_apply_curry(struct thread *thread, int nargs)
  104. {
  105.     obj_t *args = thread->sp - 3;
  106.     obj_t func = args[0];
  107.     obj_t vec1 = args[1];
  108.     obj_t vec2 = args[2];
  109.     int len1 = SOVEC(vec1)->length;
  110.     int len2 = SOVEC(vec2)->length;
  111.     int i;
  112.  
  113.     assert(nargs == 3);
  114.  
  115.     args[-1] = func;
  116.  
  117.     for (i = 0; i < len1; i++)
  118.     *args++ = SOVEC(vec1)->contents[i];
  119.     for (i = 0; i < len2; i++)
  120.     *args++ = SOVEC(vec2)->contents[i];
  121.  
  122.     thread->sp = args;
  123.  
  124.     invoke(thread, len1+len2);
  125. }
  126.  
  127.  
  128. /* Invoking the debugger. */
  129.  
  130. static void dylan_invoke_debugger(struct thread *thread, int nargs)
  131. {
  132.     obj_t *args;
  133.  
  134.     assert(nargs == 1);
  135.  
  136.     args = thread->sp - 1;
  137.     push_linkage(thread, args);
  138.  
  139.     thread_debuggered(thread, args[0]);
  140. }
  141.  
  142.  
  143. /* Init stuff. */
  144.  
  145. void init_misc_functions(void)
  146. {
  147. #if ! NO_ARGV_0
  148.     define_generic_function("main", 1, TRUE, obj_False, FALSE,
  149.                 obj_Nil, obj_ObjectClass);
  150. #else
  151.     define_generic_function("main", 0, TRUE, obj_False, FALSE,
  152.                 obj_Nil, obj_ObjectClass);
  153. #endif
  154.     define_function("raw-exit", list1(obj_FixnumClass), FALSE, obj_False,
  155.             FALSE, obj_ObjectClass, dylan_exit);
  156.     define_constant("invoke-debugger",
  157.             make_raw_function("invoke-debugger", 1, FALSE, obj_False,
  158.                       FALSE, obj_Nil, obj_ObjectClass,
  159.                       dylan_invoke_debugger));
  160.     define_constant("values",
  161.             make_raw_function("values", 0, TRUE, obj_False, FALSE,
  162.                       obj_Nil, obj_ObjectClass,
  163.                       dylan_values));
  164.     define_constant("apply",
  165.             make_raw_function("apply", 2, TRUE, obj_False, FALSE,
  166.                       obj_Nil, obj_ObjectClass,
  167.                       dylan_apply));
  168.     generic_apply_var = find_variable(module_BuiltinStuff,
  169.                       symbol("generic-apply"),
  170.                       FALSE, TRUE);
  171.     define_constant("apply-curry",
  172.             make_raw_function("apply-curry", 3, FALSE, obj_False,
  173.                       FALSE, obj_Nil, obj_ObjectClass,
  174.                       dylan_apply_curry));
  175. }
  176.